//
//  FoodClassViewController.m
//  掌厨
//
//  Created by aoyolo on 15/10/22.
//  Copyright (c) 2015年 李祖铜. All rights reserved.
//

#import "FoodClassViewController.h"

#import "AFNetworking.h"

#import "FoodHeadCell.h"
#import "FoodCourseCell.h"


#import "FoodModel.h"


@interface FoodClassViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSDictionary *foodHeads;
@property (nonatomic, strong)NSMutableArray *foodCouses;

@end

@implementation FoodClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _foodCouses = [NSMutableArray array];
    
    self.navigationController.navigationBar.barTintColor = appColor;
    //设置导航栏标题的字体颜色和大小
    self.navigationItem.title = @"食课";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [_tableView registerNib:[UINib nibWithNibName:@"FoodHeadCell" bundle:nil] forCellReuseIdentifier:@"FoodHeadCellId"];
    [_tableView registerNib:[UINib nibWithNibName:@"FoodCourseCell" bundle:nil] forCellReuseIdentifier:@"FoodCourseCellId"];
    
    //食课网络请求
    AFHTTPRequestOperationManager *foodClassStr = [AFHTTPRequestOperationManager manager];
    
    [foodClassStr POST:zhangchuURL parameters:@{@"methodName":@"CourseIndex", @"page":@"1", @"size":@"10", @"user_id":@"0", @"version":@"1.0"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //食课头图片网络数据
        self.foodHeads = responseObject[@"data"][@"top"];
        
        for (NSDictionary *dic in responseObject[@"data"][@"data"])
        {
            FoodModel *foodmodel = [[FoodModel alloc]init];
            foodmodel.title = dic[@"title"];
            foodmodel.create_time = dic[@"course"][@"create_time"];
            foodmodel.descript = dic[@"course"][@"description"];
            foodmodel.title2 = dic[@"course"][@"title"];
            foodmodel.descript2 = dic[@"course"][@"description"];
            foodmodel.video_length = dic[@"course"][@"video_length"];
            foodmodel.play = dic[@"course"][@"play"];
            foodmodel.image = dic[@"course"][@"image"];
            
            [self.foodCouses addObject:foodmodel];
        }
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    
}

#pragma mark - UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 180;
    }
    else if (indexPath.section == 1)
    {
        return 200;
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1)
    {
        return _foodCouses.count;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        FoodHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FoodHeadCellId"];
        [cell setFoodHead:_foodHeads];
        return cell;
    }
    else if (indexPath.section == 1)
    {
        FoodCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FoodCourseCellId"];
        [cell setFoodCourse:self.foodCouses[indexPath.row]];
        return cell;
    }
    return 0;
}





@end
